/**
 * Created by Alice on 2016/9/13 0013
 */
;-function (window) {
    "use strict";
    //TODO 预设变量写这里，全局变量写在base.js 里面 使用 r. 调用
    var ready = {
        layer: null,
        flag: null,
        page_size: 12,
        city: null
    };
    //TODO 初始化的方法写这里面
    var init = function () {
        init_ready();//加载预设变量
        init_event();

        function init_event() {//TODO 初始化页面事件
            var currentDay = utils.get_date(0);
            var tomorrow = utils.get_date(1);
            //查询的开始时间默认给今天
            $('#startTime').datetimepicker({
                bootcssVer: 3,
                minView: "month", //选择日期后，不会再跳转去选择时分秒
                weekStart: "1",
                format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
                language: 'zh-CN', //汉化
                autoclose: true //选择日期后自动关闭
            }).on('changeDate', function (ev) {
                console.log(ev.date.valueOf());
            });
            $('#endTime').datetimepicker({
                bootcssVer: 3,
                minView: "month", //选择日期后，不会再跳转去选择时分秒
                weekStart: "1",
                format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
                language: 'zh-CN', //汉化
                autoclose: true //选择日期后自动关闭
            }).on('changeDate', function (ev) {
                console.log(ev.date.valueOf());
            });
            if (ready.flag == 1) {
                $(".active").find("span").html("&nbsp;更多开庭");//今日开庭和排期
                $("#startTime").find("input").val(currentDay);
                $("#endTime").find("input").val(currentDay);
            } else {
                $(".active").find("span").html("&nbsp;更多预告");//明日预告
                $("#startTime").find("input").val(tomorrow);
                $("#endTime").find("input").val(tomorrow);
            }
            $(".container-fluid").show();
            Class.init();
            // Class.init_push();
            $("#selectAllChildCourts").change(function () {
                Class.child_option_change();
            });

            $("#_search").click(function () {
                Class.get_sum_case();
            });
            //点击查询按钮
            $("#conditionBtn").click(function () {
                $("#loading").show();
                Class.get_sum_case();
            });
        }

        function init_ready() {//TODO 初始化预设值
            layui.use('layer', function () {
                ready.layer = layui.layer;
            });
            ready.flag = utils.get_url_param("flag");
        }
    };
    //TODO 内部调用的方法写这里
    var Class = {
        init: function () {
            // console.info(r.court_list);
            Class.select_all_child_courts();
            Class.getCourtRoomAndType();
            Class.getJudgeAndSecretaryInit();
            Class.get_sum_case();
        },
        get_sum_case: function () {
            if (r.flag_search) {
                Class.getAllSumCaseNumForCondition();
            } else {
                Class.getSumCaseNumForCondition();
            }
        },
        child_option_change: function () {
            var _currentCourtUid = $("#selectAllChildCourts").val();
            Class.getCourtRoomAndType(_currentCourtUid);
            Class.getJudgeAndSecretary(_currentCourtUid);
        },
        select_all_child_courts: function () {
            var select_child_courts = $("#selectAllChildCourts");
            select_child_courts.empty();
            $.each(r.court_list, function (i, n) {
                select_child_courts.append("<option value='" + n.uid + "'>" + n.name + "</option>");
            });
            select_child_courts.val(r.court_uid);
            ready.city = r.court_uid;
        },
        getCourtRoomAndType: function (_currentCourtUid) {
            if (_currentCourtUid) {
                ready.city = _currentCourtUid;
            }
            var court_room = $("#allCourtRooms").empty();
            court_room.append("<option value=''>" + "所有法庭" + "</option>");
            $.each(courtroomList, function (i, valueOfElement) {
                if (valueOfElement.courtUid == ready.city && valueOfElement.active == 1) {
                    court_room.append("<option value='" + valueOfElement.uid + "'>" + valueOfElement.name + "</option>");
                }
            });

            var case_type = $("#caseTypeName").empty();
            case_type.append("<option value=''>" + "所有类型" + "</option>");
            $.each(caseTypeList, function (i, valueOfElement) {
                case_type.append("<option value='" + valueOfElement.uid + "'>" + valueOfElement.name + "</option>");
            });
        },
        getJudgeAndSecretaryInit: function () {
            var select_judge = $("#allJudges").empty();
            select_judge.append("<option value=''>" + "所有法官" + "</option>");
            $.each(judgeList, function (i, valueOfElement) {
                select_judge.append("<option value='" + valueOfElement.uid + "'>" + valueOfElement.name + "</option>");
            });
            var select_secretary = $("#allSecretary").empty();
            select_secretary.append("<option value=''>" + "所有书记员" + "</option>");
            $.each(secretaryList, function (i, valueOfElement) {
                select_secretary.append("<option value='" + valueOfElement.uid + "'>" + valueOfElement.name + "</option>");
            });
        },
        getJudgeAndSecretary: function (_currentCourtUid) {
            if (_currentCourtUid) {
                ready.city = _currentCourtUid;
            }
            $.post(r.base_path + "caseCount/getJudgeAndSecretary", {courtUid: ready.city}, function (result) {
                if (result.data) {
                    var select_judge = $("#allJudges").empty();
                    select_judge.append("<option value=''>" + "所有法官" + "</option>");
                    $.each(result.data.judgeList, function (i, valueOfElement) {
                        select_judge.append("<option value='" + valueOfElement.uid + "'>" + valueOfElement.name + "</option>");
                    });
                    var select_secretary = $("#allSecretary").empty();
                    select_secretary.append("<option value=''>" + "所有书记员" + "</option>");
                    $.each(result.data.secretaryList, function (i, valueOfElement) {
                        select_secretary.append("<option value='" + valueOfElement.uid + "'>" + valueOfElement.name + "</option>");
                    });
                }
            }, 'json');
        },
        temp: function (htmlid, id, value) {
            // console.log(id, value);
            $(htmlid).find("button").html(value + '<span class="caret"></span>');
            $(htmlid).find("input").val(id);
        },
        getSumCaseNumForCondition: function (page) {
            if (!page) {
                page = 1;
            }
            Class.page(1, 1, null);
            $('#loading').show();
            //条件
            var caseno = $("#caseno").val();
            var caseName = $("#caseName").val();
            var caseTypeName = $("#caseTypeName").val();
            var courtRoom = $("#allCourtRooms").val();
            var judge = $("#allJudges").val();
            var secretary = $("#allSecretary").val();
            var startTime = $("#startTime").find("input").val();
            var endTime = $("#endTime").find("input").val();
            var row_no_padding = $(".row-no-padding").empty();

            // console.log(caseName, caseTypeName, courtRoom, judge, secretary, startTime, endTime);
            $.post(r.base_path + "review/countReviewOrPlan", {
                archives: caseno,
                caseName: caseName,
                type: caseTypeName,
                courtRoom: courtRoom,
                judgeId: judge,
                secretaryId: secretary,
                startDate: startTime,
                endDate: endTime,
                courtUid: ready.city,
                status: 0,
                review: 0,
                page: page,
                pageSize: ready.page_size
            }, function (result) {
                $('#loading').hide();
                if (result && result.code == 0) {
                    if (result.data <= 0) {
                        $('#loading').hide();
                    } else {
                        Class.page(page, Math.ceil(result.data / 12), Class.getSumCaseNumForCondition);
                    }
                } else {

                }
            }, 'json').fail(function () {
                $('#loading').hide();
            });

            $.ajax({
                url: r.base_path + "review/getReviewOrPlan",
                type: "post",
                dataType: "json",
                data: {
                    archives: caseno,
                    caseName: caseName,
                    type: caseTypeName,
                    courtRoom: courtRoom,
                    judgeId: judge,
                    secretaryId: secretary,
                    startDate: startTime,
                    endDate: endTime,
                    courtUid: ready.city,
                    status: 0,
                    review: 0,
                    page: page,
                    pageSize: ready.page_size
                },
                success: function (result) {
                    //清空表格
                    if (result.code == 0) {
                        if (result.data.count <= 0) {
                            row_no_padding.append('<span class="more-errmsg">暂无开庭信息</span>');
                        } else {
                            var picNum = 6;
                            $.each(result.data.data, function (i, valueOfElement) {
                                var caseName = valueOfElement.name;
                                var startDate = valueOfElement.startDate;
                                var uid = valueOfElement.planUid;
                                // startDate = startDate.substring(0, startDate.length - 2);
                                var status = valueOfElement.status;
                                var courtroom_name = valueOfElement.courtroomName;
                                var cid = valueOfElement.courtroomUid;
                                var statusName = "";
                                if (0 == status) {
                                    statusName = "排期";
                                } else if (1 == status || 5 == status) {
                                    statusName = "开庭";
                                } else if (2 == status) {
                                    statusName = "休庭";
                                } else if (3 == status) {
                                    statusName = "闭庭";
                                } else {
                                    statusName = "归档";
                                }
                                var randomNum = parseInt(picNum * Math.random() + 1);//随机出1到picNum的整数
                                var href = r.base_path + "system_monitor/panel_living?planUid=" + uid + "&courtRoomUid=" + cid + "&vlcCourtUid=" + valueOfElement.courtUid + '&courtUid=' + r.court_uid;
                                var html = '<div class="col-md-2 col-xs-6">' +
                                    '<div class="more-courting-item">' +
                                    '<a href="' + href + '">' +
                                    '<div class="more-courting-item-img-panel">' +
                                    '<p class="more-courting-item-img-panel-top">' + (courtroom_name ? courtroom_name : '未知') + '</p>' +
                                    '<img src="' + r.base_path + "assets/img/more_court_test" + randomNum + ".jpg" + '" alt="法院">' +
                                    '<p class="more-courting-item-img-panel-bottom" >' + statusName + '</p>' +
                                    '</div>' +
                                    '<p id="startDate">' + startDate + '</p>' +
                                    '<p id="caseNoDis">' + (valueOfElement.archives ? valueOfElement.archives : '未知') + '</p>' +
                                    '<p class="more-courting-item-depiction">' +
                                    // '<i class="fa fa-exchange cursor-hand" title="推送" id="'+valueOfElement.uid+'"></i>'+
                                    '<span>' + (caseName ? caseName : "&nbsp;") + '</span>' +
                                    '</p>' +
                                    '</a>' +
                                    '</div>' +
                                    '</div>';
                                row_no_padding.append(html);
                            });
                            $('.fa-exchange').click(function (e) {
                                e.preventDefault();
                                // console.log();
                                $('#_uid').val(this.id);
                                $("#_detail").modal("show");
                            })
                        }
                        row_no_padding.show();
                    }
                },
                error: function () {
                }
            });
        },
        getAllSumCaseNumForCondition: function (page) {
            if (!page) {
                page = 1;
            }
            Class.page(1,1,null);
            //条件
            var caseno = $("#caseno").val();
            var caseName = $("#caseName").val();
            var caseTypeName = $("#caseTypeName").val();
            var courtRoom = $("#allCourtRooms").val();
            var judge = $("#allJudges").val();
            var secretary = $("#allSecretary").val();
            var startTime = $("#startTime").find("input").val();
            var endTime = $("#endTime").find("input").val();
            var row_no_padding = $(".row-no-padding").empty();
            console.log(caseName, caseTypeName, courtRoom, judge, secretary, startTime, endTime);

            $.post(r.base_path + "review/countReviewOrPlan", {
                archives: caseno,
                caseName: caseName,
                type: caseTypeName,
                courtRoom: courtRoom,
                judgeId: judge,
                secretaryId: secretary,
                startDate: startTime,
                endDate: endTime,
                courtUid: ready.city,
                status: 1,
                review: 0,
                page: page,
                pageSize: ready.page_size
            }, function (result) {
                $('#loading').hide();
                if (result && result.code == 0) {
                    if (result.data <= 0) {
                        $('#loading').hide();
                    } else {
                        Class.page(page, Math.ceil(result.data / 12), Class.getAllSumCaseNumForCondition);
                    }
                } else {

                }
            }, 'json').fail(function () {
                $('#loading').hide();
            });

            $.ajax({
                url: r.base_path + "review/getReviewOrPlan",
                type: "post",
                dataType: "json",
                data: {
                    archives: caseno,
                    caseName: caseName,
                    type: caseTypeName,
                    courtRoom: courtRoom,
                    judgeId: judge,
                    secretaryId: secretary,
                    startDate: startTime,
                    endDate: endTime,
                    courtUid: ready.city,
                    status: 1,
                    review: 0,
                    page: page,
                    pageSize: ready.page_size
                },
                success: function (result) {
                    //清空表格
                    if (result.code == 0) {
                        if (result.data.count <= 0) {
                            row_no_padding.append('<span class="more-errmsg">暂无开庭信息</span>');
                        } else {
                            var picNum = 6;
                            $.each(result.data.data, function (i, valueOfElement) {
                                var caseName = valueOfElement.name;
                                var startDate = valueOfElement.startDate;
                                var uid = valueOfElement.planUid;
                                // startDate = startDate.substring(0, startDate.length - 2);
                                var status = valueOfElement.status;
                                var courtroom_name = valueOfElement.courtroomName;
                                var cid = valueOfElement.courtroomUid;
                                var statusName = "";
                                if (0 == status) {
                                    statusName = "排期";
                                } else if (1 == status || 5 == status) {
                                    statusName = "开庭";
                                } else if (2 == status) {
                                    statusName = "休庭";
                                } else if (3 == status) {
                                    statusName = "闭庭";
                                } else {
                                    statusName = "归档";
                                }
                                var randomNum = parseInt(picNum * Math.random() + 1);//随机出1到picNum的整数
                                var href = r.base_path + "system_monitor/panel_living?planUid=" + uid + "&courtRoomUid=" + cid + "&vlcCourtUid=" + valueOfElement.courtUid + '&courtUid=' + r.court_uid;
                                var html = '<div class="col-md-2 col-xs-6">' +
                                    '<div class="more-courting-item">' +
                                    '<a href="' + href + '">' +
                                    '<div class="more-courting-item-img-panel">' +
                                    '<p class="more-courting-item-img-panel-top">' + (courtroom_name ? courtroom_name : '未知') + '</p>' +
                                    '<img src="' + r.base_path + "assets/img/more_court_test" + randomNum + ".jpg" + '" alt="法院">' +
                                    '<p class="more-courting-item-img-panel-bottom" >' + statusName + '</p>' +
                                    '</div>' +
                                    '<p id="startDate">' + startDate + '</p>' +
                                    '<p id="caseNoDis">' + (valueOfElement.archives ? valueOfElement.archives : '未知') + '</p>' +
                                    '<p class="more-courting-item-depiction">' +
                                    // '<i class="fa fa-exchange cursor-hand" title="推送" id="'+valueOfElement.uid+'"></i>'+
                                    '<span>' + (caseName ? caseName : "&nbsp;") + '</span>' +
                                    '</p>' +
                                    '</a>' +
                                    '</div>' +
                                    '</div>';
                                row_no_padding.append(html);
                            });
                            $('.fa-exchange').click(function (e) {
                                e.preventDefault();
                                // console.log();
                                $('#_uid').val(this.id);
                                $("#_detail").modal("show");
                            })
                        }
                        row_no_padding.show();
                    }
                },
                error: function () {
                    $("#loading").hide();
                }
            });
        },
        init_push: function () {
            var court_push = $("#_courtPush").empty();
            $(r.court_list).each(function (i, n) {
                court_push.append("<option value='" + n.courtid + "'>" + n.courtname + "</option>");
            });
            Class.select_courtroom(r.court_list[0].courtid);
            court_push.change(function () {
                var val = $(this).children('option:selected').val();
                Class.select_courtroom(val);
            })
        },
        select_courtroom: function (id) {
            $.post(r.base_path + "sys/courtEquipment/selectCourtRoom", {courtId: id}, function (result) {
                if (result.data) {
                    $("#_courtRoom").empty();
                    $.each(result.data, function (i, n) {
                        $("#_courtRoom").append("<option value='" + n.uid + "'>" + n.courtroomname + "</option>");
                    });
                }
            }, 'json')
        },
        save: function () {
            layui.use('layer', function () {
                var layer = layui.layer;
                layer.confirm('想要推送该案件吗?<br>将要推送到:<br>"' +
                    $('#_courtPush').find("option:selected").text() + '<br>' + $('#_courtRoom').find("option:selected").text() + '"', {
                    btn: ['是的', '取消'] //按钮
                }, function () {
                    $.post(r.base_path + "planTransfer/planTransferDown", {
                        planid: $('#_uid').val(),
                        downcourtid: $('#_courtPush').val(),
                        courtroomid: $('#_courtRoom').val()
                    }, function (result) {
                        if (result && result.status == 0) {
                            layer.msg('推送成功！');
                        } else {
                            layer.msg(result.msg);
                        }
                        $("#_detail").modal("hide");
                    }, 'json').fail(function () {
                        $("#_detail").modal("hide");
                    })
                }, function () {

                });
            });
        },
        page: function (now, totalPages, callback) {
            base.pagination('#_page', {
                currentPage: now,
                totalPages: totalPages,
                onPageClicked: function (page) {
                    callback(page);
                }
            });
        }

    };
    init();
    //TODO 对外公开的方法写在这里
    window.p = {
        get_sum_case: Class.get_sum_case,
        save: Class.save
    };
}(window);